﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraBars;
using DevExpress.XtraBars.Helpers;
using DevExpress.XtraBars.Docking2010.Views;
using DevExpress.XtraEditors;
using Model;

namespace WindowsForms
{
    public partial class Main : DevExpress.XtraBars.Ribbon.RibbonForm
    {
        private IDictionary<string, BaseDocument> documentsCore = new Dictionary<string, BaseDocument>();
        public static Staffs STAFF_IDENTITY;

        public IDictionary<string, BaseDocument> Documents
        {
            get
            {
                return documentsCore;
            }
        }

        public Main(Staffs staff)
        {
            InitializeComponent();

            InitSkins();

            documentTabViewer.DocumentAdded += (sender, agrs) =>
            {
                if (agrs.Document.Form != null)
                {
                    Documents.Add(agrs.Document.Form.Name, agrs.Document);
                }
            };
            documentTabViewer.DocumentRemoved += (sender, agrs) =>
            {
                if (agrs.Document.Form != null)
                {
                    Documents.Remove(agrs.Document.Form.Name);
                }
            };
            STAFF_IDENTITY = staff;
        }

        private void ActiveForms(XtraForm frm)
        {
            BaseDocument document = null;
            if (!this.Documents.TryGetValue(frm.Name, out document))
            {
                frm.MdiParent = this;
                frm.Show();
                if (documentTabViewer.Documents.TryGetValue(frm, out document))
                {
                    document.Caption = frm.Name;
                }
            }
            else
                documentTabViewer.Controller.Activate(document);
        }

        private void InitSkins()
        {
            SkinHelper.InitSkinGallery(lstSkins, true);
        }

        public void OpenForm(XtraForm fopen, object[] paras)
        {
            try
            {
                Boolean bCheck = true;
                foreach (Form f in this.MdiChildren)
                {
                    if (fopen.Name == f.Name)
                    {
                        bCheck = false;
                        f.Activate();
                        break;
                    }
                }
                if (bCheck)
                {
                    Form f1 = GetFormByName(fopen.Name, paras);
                    if (f1 != null)
                    {
                        f1.MdiParent = this;
                        f1.Show();
                    }
                }
            }
            catch
            {
                XtraMessageBox.Show("Chức Năng Này Đang Xây Dựng ", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Warning);
            }
        }

		private Form GetFormByName(string fopen, object[] paras)
        {

                Type T = Type.GetType(fopen, false);
                try
                {
                    if (T == null)
                    {
                        string Fullname = Application.ProductName + "." + fopen;
                        T = Type.GetType(Fullname, true, true);
                    }
                    if (paras != null)
                    {
                        return (Form)Activator.CreateInstance(T, paras);
                    }
                    else
                    {
                        return (Form)Activator.CreateInstance(T);
                    }
                }
                catch
                {
                    XtraMessageBox.Show("Chức Năng Này Đang Xây Dựng ", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                }
                finally
                {
                }
            
            return null;
        }
		
        private void btnLoss_ItemClick(object sender, ItemClickEventArgs e)
        {
            if (MessageBox.Show("Bạn thật sự muốn thoát ?", "Hệ thống", MessageBoxButtons.YesNo) == DialogResult.Yes)
            {
                this.Close();
            }
        }

        private void btnUnit_ItemClick(object sender, ItemClickEventArgs e)
        {
            UnitManagement frmUnit = new UnitManagement();
            OpenForm(frmUnit, null);
        }

        private void btnMenus_ItemClick(object sender, ItemClickEventArgs e)
        {
            MenuManagement frm = new MenuManagement();
            OpenForm(frm, null);
        }

        private void btnMenuTypes_ItemClick(object sender, ItemClickEventArgs e)
        {
            MenuTypeManagement frm = new MenuTypeManagement();
            OpenForm(frm, null);
        }

        private void btnRegions_ItemClick(object sender, ItemClickEventArgs e)
        {
            RegionManagement frm = new RegionManagement();
            OpenForm(frm, null);
        }

        private void btnTables_ItemClick(object sender, ItemClickEventArgs e)
        {
            TableManagement frm = new TableManagement();
            OpenForm(frm, null);
        }

        private void btnStaffs_ItemClick(object sender, ItemClickEventArgs e)
        {
            StaffManagement frm = new StaffManagement();
            OpenForm(frm, null);
        }

        private void btnOrder_ItemClick(object sender, ItemClickEventArgs e)
        {
            SalesManagement frm = new SalesManagement();
            OpenForm(frm, null);
        }

        private void btnHistorys_ItemClick(object sender, ItemClickEventArgs e)
        {
            HistoryManagement frm = new HistoryManagement();
            OpenForm(frm, null);
        }

        private void barButtonItem1_ItemClick(object sender, ItemClickEventArgs e)
        {
            rptHistoryBooking frm = new rptHistoryBooking();
            OpenForm(frm, null);
        }

        private void barButtonItem2_ItemClick(object sender, ItemClickEventArgs e)
        {
            OpenForm(null, null);
        }
    }
}